package com.pet.server.ext.action.square;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.BeanToJson;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Image;
import com.pet.server.ext.data.PetMissing;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.DateUtil;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Missingpublish extends ActionBase{
	private PetMissing petMissing;
	private List<String> images;
	
	public Missingpublish(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		images=new ArrayList<String>();
		petMissing = new PetMissing();
		Long date = NumberUtil.getLong(params.getFieldValue("miss_date"),0l);
		//petMissing.ico_path=params.getFileUrl("ico_path");
		petMissing.age=StringUtil.dealNull(params.getFieldValue("age"))  ;
		petMissing.breed=StringUtil.dealNull(params.getFieldValue("breed"));
		petMissing.description=StringUtil.dealNull(params.getFieldValue("description"));
		petMissing.pet_cate=StringUtil.dealNull(params.getFieldValue("pet_cate"));
		petMissing.miss_date=DateUtil.getDate(date);//  ()DateUtil.getDate(params.getFieldValue("miss_date"));
		petMissing.sex=StringUtil.dealNull(params.getFieldValue("sex"));
		petMissing.feature=StringUtil.dealNull(params.getFieldValue("feature"));
		petMissing.city=StringUtil.dealNull(params.getFieldValue("city"));
		petMissing.city_name=StringUtil.filterCityName(params.getFieldValue("city_name"));
		petMissing.area=StringUtil.dealNull(params.getFieldValue("area"));
		petMissing.longitude=NumberUtil.getDouble(params.getFieldValue("longitude"),0d);
		petMissing.latitude=NumberUtil.getDouble(params.getFieldValue("latitude"),0d);
		petMissing.phone=StringUtil.dealNull(params.getFieldValue("phone"));
		int i=0;
		while(params.getFileUrl("image"+i)!=null){
			images.add(params.getFileUrl("image"+i));
			i++;
		}
		if(images.size()>0){
			petMissing.ico_path=images.get(0);
		}
        if(StringUtil.isEmpty(petMissing.pet_cate,petMissing.breed)){
        	throw ApiError.Type.INVALID_PARAM.toException();
        }
	}
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		petMissing.user_id=user.user_id;
		userService.savePetMissing(petMissing);
		
		
		List<Image> list = new ArrayList<Image>();
		for(String image:images){
			Image img = new Image();
			img.type=BaseAttribute.image_type.pet_missing.toString();
			img.src=image;
			img.oid=petMissing.miss_id;
			userService.saveImage(img);
			list.add(img);
		}
		petMissing.list=list;
        JSONObject o = JsonUtil.missingToJson(petMissing);
		Result result = new Result();
		result.put("data", o);
	    return result;
	}


}
